package org.jetlinks.pro.tenant;

import lombok.Getter;
import lombok.Setter;
import org.hswebframework.web.authorization.Authentication;
import org.jetlinks.pro.assets.AssetsHolderProvider;
import org.jetlinks.pro.authorize.OrgDimensionType;
import org.reactivestreams.Publisher;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

@Component
@ConditionalOnProperty(prefix = "jetlinks.assets.tenant-member", name = "enabled", havingValue = "true", matchIfMissing = true)
@ConfigurationProperties(prefix = "jetlinks.assets.tenant-member")
@Getter
@Setter
public class TenantAssetsHolderProvider implements AssetsHolderProvider {

    /**
     * 租户内用户查看所有的资产
     */
    private Set<String> allowAllAssets;

    /**
     * 忽略处理的资产
     */
    private Set<String> ignoreAssets;

    /**
     * 属于资产的维度
     */
    private Set<String> dimensionAssets = new HashSet<>(Collections.singletonList(OrgDimensionType.org.getId()));

    /**
     * 资产管理器
     */
    private final TenantAssetManager assetManager;

    public TenantAssetsHolderProvider(TenantAssetManager assetManager) {
        this.assetManager = assetManager;
    }

    @Override
    public Publisher<TenantAssetsHolder> createHolder(Authentication authentication) {
        return TenantMember
            .fromAuth(authentication)
            .map(member -> new TenantAssetsHolder(member, assetManager, allowAllAssets, ignoreAssets, dimensionAssets))
            ;
    }

}
